package com.topisv.tms.utils.osa;

import java.util.ArrayList;
import java.util.List;

import com.topisv.tms.service.vo.PackItems;
import com.topisv.tms.service.vo.PackStyleModel;
import com.topisv.tms.utils.PackStyleTablesOSA;
import com.topisv.tms.utils.SystemUtil;
import com.topisv.tms.utils.printModel.CommPackStyleModel;

/**
 * 生成OSA模版
 * @author Administrator
 *
 */
public class OsaPrinterModel {

	/**
	 * OSA模版
	 * 
	 * @param content
	 * @param packStyleModel
	 * @return
	 */
	public static String getOSAPackStyleContent(String content,
			PackStyleModel packStyleModel) {
		StringBuffer sb = new StringBuffer(
				PackStyleTablesOSA.osaFirstTable(packStyleModel));
		int length = getPackformStyleContent(content);
		String bodyString = getOSABodyContent(content, packStyleModel);
		
	/*	int index=content.indexOf(">");
	    String tempIndex=content.substring(0, index);
	    tempIndex+="><font size='6'>"+packStyleModel.getRankno()+"</font>";
	    String tempContent=tempIndex+content.substring(index+1);*/
		sb
				.append("</br></br>"+PackStyleTablesOSA.osaTopTable(length, packStyleModel))
				.append(content).append(PackStyleTablesOSA.osaMiddleTable())
				.append(bodyString)
				.append(PackStyleTablesOSA.osaEndTable(length, packStyleModel));
		return sb.toString();
	}
	
	public static int getPackformStyleContent(String content) {
		String[] rowspans = content.split("rowspan");
		int conlsLength = rowspans.length - 1;
		return conlsLength;
	}
	
	public static String getOSABodyContent(String content,
			PackStyleModel packStyleModel) {
		List<PackItems> packItems = packStyleModel.getPackItems();
		StringBuffer tempBodyContentString = new StringBuffer();
		String[] bodyContents =CommPackStyleModel.getBodyContentString(content.replace(SystemUtil.getLineSeparator(), ""));
        List<String> initTest=new ArrayList<String>();
		for (int i = 0; i < packItems.size(); i++) {
			PackItems packItem = packItems.get(i);
			StringBuffer bodyContentString = new StringBuffer(
					"<tr style='height: 25px'>");
			bodyContentString.append("<td><div align='center'>"
					+ PackStyleTablesOSA.getContent(packItem.getItemCode())
					+ "</div></td>");


			for (int l = 0; l < bodyContents.length; l++) {
				if (bodyContents[l] != "") {
					if (bodyContents[l].equals("规格")) {
						bodyContentString.append("<td><div align='center'>"
								+ PackStyleTablesOSA.getContent(packItem
										.getFormat()) + "</div></td>");
					} else if (bodyContents[l].equals("数量")) {
						bodyContentString.append("<td><div align='center'>"
								+ PackStyleTablesOSA.getContent(packItem
										.getNumber()) + "</div></td>");
					}  else if (bodyContents[l].equals("品名")) {
						bodyContentString.append("<td><div align='center'>"
								+ PackStyleTablesOSA.getContent(packItem
										.getItemName()) + "</div></td>");
					} else if (bodyContents[l].equals("折扣")) {
						bodyContentString.append("<td><div align='center'>"
								+ PackStyleTablesOSA.getContent(packItem
										.getDis()) + "</div></td>");
					} else if (bodyContents[l].equals("单价")) {
						bodyContentString.append("<td><div align='center'>"
								+ PackStyleTablesOSA.getContent(packItem
										.getPrice()) + "</div></td>");
					}  else if (bodyContents[l].equals("应付金额")) {
						bodyContentString.append("<td><div align='center'>"
								+ PackStyleTablesOSA.getContent(packItem
										.getPay()) + "</div></td>");
					} else if (bodyContents[l].equals("实际金额")) {
						bodyContentString.append("<td><div align='center'>"
								+ PackStyleTablesOSA.getContent(packItem
										.getAmo()) + "</div></td>");
					} else if (bodyContents[l].equals("优惠金额")) {
						bodyContentString.append("<td><div align='center'>"
								+ PackStyleTablesOSA.getContent(packItem
										.getPromotion()) + "</div></td>");
					} else if (bodyContents[l].equals("颜色")) {
						bodyContentString.append("<td><div align='center'>"
								+ (PackStyleTablesOSA.getContent(packItem
										.getColor())) + "</div></td>");
					}
				}
			}

			for (int n = 0; n < 8; n++) {
				bodyContentString.append("<td></td>");
			}

			bodyContentString.append("</tr>");
			tempBodyContentString.append(bodyContentString);
		}
		for(int i=0;i<bodyContents.length;i++){
			if(!"".equals(bodyContents[i])){
		    	initTest.add(bodyContents[i]);
			}
		}
		StringBuffer tempString=new StringBuffer("<tr style='height: 25px'>");
		tempString.append("<td><div align='center'>标记</div></td>");
        tempString.append("<td colspan='"+(initTest.size()-2)+"'><div align='right'>       |总计:</div></td>");
        tempString.append("<td><div align='center'>"+packStyleModel.getNumber()+"</div></td>");
        tempString.append("<td><div align='center'>"+packStyleModel.getAllTotal()+"</div></td>");
    	for (int n = 0; n < 8; n++) {
    		tempString.append("<td></td>");
		}
        tempString.append("</tr>");
		tempBodyContentString.append(tempString);

		return tempBodyContentString.toString();
	}
	
	
	public static String getOSABodyContentTest(String content,
			PackStyleModel packStyleModel) {
		List<PackItems> packItems = packStyleModel.getPackItems();
		StringBuffer tempBodyContentString = new StringBuffer();
		int number = 0;
		int allPrice = 0;
		for (int i = 0; i < packItems.size(); i++) {
			PackItems packItem = packItems.get(i);
			StringBuffer bodyContentString = new StringBuffer(
					"<tr style='height: 25px'>");
			bodyContentString.append("<td>"
					+ PackStyleTablesOSA.getContent(packItem.getItemCode())
					+ "</td>");

			String[] bodyContents =CommPackStyleModel.getBodyContentString(content);

			for (int l = 0; l < bodyContents.length; l++) {
				if (bodyContents[l] != "") {
					if (bodyContents[l].equals("规格")) {
						bodyContentString.append("<td>"
								+ PackStyleTablesOSA.getContent(packItem
										.getFormat()) + "</td>");
					} else if (bodyContents[l].equals("数量")) {
						bodyContentString.append("<td>"
								+ PackStyleTablesOSA.getContent(packItem
										.getNumber()) + "</td>");
					} else if (bodyContents[l].equals("商品总数量") && number == 0) {
						number = 1;
						bodyContentString.append("<td  rowspan='"
								+ packItems.size()
								+ "'> "
								+ PackStyleTablesOSA.getContent(packStyleModel
										.getNumber()) + "</td>");
					} else if (bodyContents[l].equals("品名")) {
						bodyContentString.append("<td>"
								+ PackStyleTablesOSA.getContent(packItem
										.getItemName()) + "</td>");
					} else if (bodyContents[l].equals("折扣")) {
						bodyContentString.append("<td>"
								+ PackStyleTablesOSA.getContent(packItem
										.getDis()) + "</td>");
					} else if (bodyContents[l].equals("单价")) {
						bodyContentString.append("<td>"
								+ PackStyleTablesOSA.getContent(packItem
										.getPrice()) + "</td>");
					} else if (bodyContents[l].equals("订单总价") && allPrice == 0) {
						allPrice = 1;
						bodyContentString.append("<td rowspan='"
								+ packItems.size()
								+ "'> "
								+ PackStyleTablesOSA.getContent(packStyleModel
										.getAllTotal()) + "</td>");
					} else if (bodyContents[l].equals("应付金额")) {
						bodyContentString.append("<td>"
								+ PackStyleTablesOSA.getContent(packItem
										.getPay()) + "</td>");
					} else if (bodyContents[l].equals("实际金额")) {
						bodyContentString.append("<td >"
								+ PackStyleTablesOSA.getContent(packItem
										.getAmo()) + "</td>");
					} else if (bodyContents[l].equals("优惠金额")) {
						bodyContentString.append("<td >"
								+ PackStyleTablesOSA.getContent(packItem
										.getPromotion()) + "</td>");
					} else if (bodyContents[l].equals("颜色")) {
						bodyContentString.append("<td >"
								+ (PackStyleTablesOSA.getContent(packItem
										.getColor())) + "</td>");
					}
				}
			}

			for (int n = 0; n < 8; n++) {
				bodyContentString.append("<td></td>");
			}

			bodyContentString.append("</tr>");
			tempBodyContentString.append(bodyContentString);
		}
		return tempBodyContentString.toString();
	}
}
